package com.shenglue.ssi.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.auxiliary.SettleRemain;
import com.shenglue.ssi.domain.BuyInSettle;
import com.shenglue.ssi.domain.BuyInMain;

import com.shenglue.ssi.domain.User;
import com.shenglue.ssi.service.BuyInSettleService;
import com.shenglue.ssi.service.BuyInMainService;
import com.shenglue.ssi.service.UserService;

public class BuyInSettleAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private BuyInMain buyInMain;		// 主表记录的 Bean
	private BuyInSettle buyInSettle;	// 对应的 Bean
	private int buyInMainId;			// 所属的 主表记录 之Id
	private int buyInSettleId;			// 删除/修改 的 记录之Id
	private BuyInMainService   buyInMainService;   // 依赖的 Service（辅）
	private UserService		   userService;   	   // 依赖的 Service（辅）
	private BuyInSettleService buyInSettleService; // 依赖的 Service（主）

	private SettleRemain settleRemain = new SettleRemain();  // 计算是否结清

	/**
	 * 查询所有入库结账单信息方法
	 */
	public String queryAll() throws Exception {
		List<BuyInSettle> list = buyInSettleService.selectAllBuyInSettle( buyInMainId );
		if( list == null ) {
			// 其实不用创建，后面的JSP 够聪明  list = new ArrayList<BuyInSettle>();
			// 不能认为是错误，因为一开始是没有明细的  return INPUT;
		}

		buyInMain = buyInMainService.getOneBuyInMainForUpdate( buyInMainId );
		if( buyInMain == null ) {
			return INPUT;
		}

		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute( "buyInSettleList", list );
		request.setAttribute( "buyInMain", buyInMain );
		
		calcSettleRemain();

		return SUCCESS;
	}

	/**
	 * 增加入库结账单记录方法
	 */
	public String addNew() throws Exception {
		buyInSettle.setOperator( "" ); // 先填这个，日后从 Session 取
		boolean flag = buyInSettleService.saveBuyInSettle( buyInSettle );
		if(flag==true){
			return SUCCESS;
		}else{
			// 这2句 ，无法传递给 JSP，只能另想它法
			// HttpServletRequest request = ServletActionContext.getRequest();
			// request.setAttribute( "error", "增加明细信息失败，请检查商品信息是否重复" );

			errMsg = "增加明细信息失败，请检查商品信息是否重复";
			// 此句移到 errMsg 的 get 方法中了 ：errMsg = java.net.URLEncoder.encode( errMsg, "utf-8" );
			return ERROR;
		}
	}

	/**
	 * 删除入库结账单记录方法
	 */
	public String delete() throws Exception {

		boolean flag = buyInSettleService.deleteBuyInSettle( buyInSettleId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改入库结账单记录方法
	 */
	public String update() throws Exception {
		buyInSettle.setOperator( "" ); // 先填这个，日后从 Session 取
		boolean flag = buyInSettleService.updateBuyInSettle( buyInSettle );
		if( flag == true ) {
			return SUCCESS;
		}else{
			// errMsg = "修改明细信息失败，试一下能否显示出来——能显示";
			return ERROR;
		}

	}

	/**
	 * 准备 转向 新增 入库结账单记录 的界面
	 */
	public String prepareAddBuyInSettle() {
		
		calcSettleRemain();

		boolean flag = selectAllUser();
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 准备 转向 修改 入库结账单记录 的界面
	 */
	public String prepareModBuyInSettle() {
		calcSettleRemain();

		// 应先执行 这一段 取得 buyInSettle， 以 顺便获得 buyInMainId。因为 调本方法 的 url 不传递 buyInMainId 进来
		buyInSettle = buyInSettleService.getOneBuyInSettleForUpdate( buyInSettleId );
		if( buyInSettle == null ) {
			return ERROR;
		}

		 boolean flag = selectAllUser();
		 if( flag == false ) {
		 	return ERROR;
		 }

		return SUCCESS;
	}

	/**
	 * 预先查好 商品，为了制作下拉列表框   —— 当前入库单 有且仅有一个供应商，查该供应商的 所有商品
	 */
	private boolean selectAllUser() {

		// 得到 allUser，置于 request
		boolean bRtn;
		List<User> allUser = userService.selectUser();
		if( allUser != null ){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "allUser", allUser );
			bRtn = true;
		}else{
			bRtn = false;
		}
		return bRtn;
	}

	private void calcSettleRemain() {  // 计算  settleRemain
		float needSettle = buyInSettleService.getOneBuyInMainNeedSettle( buyInMainId );
		float  hasSettle = buyInSettleService.getOneBuyInMainHasSettle(  buyInMainId );
		settleRemain.setNeedSettle( needSettle );
		settleRemain.setHasSettle (  hasSettle );
	}
	
	public BuyInSettleAction() {
	}

	public BuyInMain getBuyInMain() {
		return buyInMain;
	}

	public void setBuyInMain(BuyInMain buyInMain) {
		this.buyInMain = buyInMain;
	}

	public BuyInSettle getBuyInSettle() {
		return buyInSettle;
	}

	public void setBuyInSettle(BuyInSettle buyInSettle) {
		this.buyInSettle = buyInSettle;
	}

	public int getBuyInMainId() {
		return buyInMainId;
	}

	public void setBuyInMainId(int buyInMainId) {
		this.buyInMainId = buyInMainId;
	}

	public int getBuyInSettleId() {
		return buyInSettleId;
	}

	public void setBuyInSettleId(int buyInSettleId) {
		this.buyInSettleId = buyInSettleId;
	}


	public BuyInMainService getBuyInMainService() {
		return buyInMainService;
	}

	public void setBuyInMainService(BuyInMainService buyInMainService) {
		this.buyInMainService = buyInMainService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public BuyInSettleService getBuyInSettleService() {
		return buyInSettleService;
	}

	public void setBuyInSettleService(BuyInSettleService buyInSettleService) {
		this.buyInSettleService = buyInSettleService;
	}

	public SettleRemain getSettleRemain() {
		return settleRemain;
	}

	public void setSettleRemain(SettleRemain settleRemain) {
		this.settleRemain = settleRemain;
	}


}
